package com.hnhcyy.goods.service.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.hnhcyy.goods.mapper.GoodsExtendMapper;
import com.hnhcyy.goods.mapper.GoodsMapper;
import com.hnhcyy.goods.mapper.SpBalMapper;
import com.hnhcyy.goods.pojo.SpBal;
import com.hnhcyy.goods.service.SearchService;

import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.Delete;
import io.searchbox.core.DocumentResult;
import io.searchbox.core.Get;
import io.searchbox.core.Index;
import io.searchbox.core.Search;

@Service
public class SearchServiceImpl implements SearchService {
	
	@Autowired 
	private JestClient jestClient;
	@Autowired 
	private GoodsMapper goodsMapper;
	@Autowired 
	private GoodsExtendMapper goodsExtendMapper;
	@Autowired 
	private SpBalMapper spBalMapper;
	@Autowired 
	private Gson gson;

	public HashMap<?, ?> get(int sjid,String spid) {
		long startTime=System.currentTimeMillis();//记录开始时间
		HashMap goods = goodsMapper.get(spid);		
		/*
		HashMap goodsExtend = goodsExtendMapper.get(sjid,spid);
		if(goodsExtend!=null) {
			String _id = goodsExtend.get("spid").toString()+"_"+goodsExtend.get("sjid").toString();
			Index index2 = new Index.Builder(goodsExtend).index("goods_extend").type("goods_extend").id(_id).build();
			try {
				DocumentResult result = jestClient.execute(index2);	
				System.out.println(result);
			} catch (IOException e) {
				e.printStackTrace();			
			}
			goods.put("extend", goodsExtend);
		}
		*/
		long endTime=System.currentTimeMillis();//记录结束时间
		long excTime=endTime-startTime;
		System.out.println("get from mysql:"+excTime+"ms");
		return goods;
	}
	
	public void set(int sjid,String spid,HashMap goods) {
		if(goods==null) {
			goods = get(sjid,spid);
		}
		elset(goods);
	}
	
	public void del(int sjid,String spid)  throws IOException{
		eldel(sjid,spid);
	}
	
	//elsearch get
	public HashMap elget(int sjid,String spid){		
		long startTime=System.currentTimeMillis();//记录开始时间
		String id = spid+"-"+sjid;
		Get get = new Get.Builder("goods", id).type("goods").build();
		try {
			JestResult result = jestClient.execute(get);
			System.out.println(result);
			HashMap ret = result.getSourceAsObject(HashMap.class);
			long endTime=System.currentTimeMillis();//记录结束时间
			long excTime=endTime-startTime;
			System.out.println("get from elsearch:"+excTime+"ms");
			return ret;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;		
	}
	
	public boolean elset(int sjid,String spid) {
		HashMap goods = goodsMapper.get(spid);
		HashMap goodsExtend = goodsExtendMapper.get(sjid, spid);
		SpBal spBal = spBalMapper.selectItemById(spid);
		int spHasBal = 1;
		if(spBal==null) {
			spHasBal = 0;
		}else {
			double kxshl = Double.parseDouble(spBal.getKxshl().toString());
			double minshl = Double.parseDouble(goods.get("minshl").toString());
			if(kxshl<minshl) {
				spHasBal = 0;
			}
		}
		goods.put("ext", goodsExtend);
		goods.put("hasBal", spHasBal);
		goods.put("bal", spBal);
		String id = spid+"-"+sjid;
		Index index = new Index.Builder(goods).index("goods").type("goods").id(id).build();
		try {
			DocumentResult result = jestClient.execute(index);		
			System.out.println(result);
			return true;
		} catch (IOException e) {
			e.printStackTrace();			
		}
		return false;
	}
	
	public boolean elsetall() {
		List<HashMap> list = goodsExtendMapper.getall();
		for(HashMap item : list) {
			int sjid = Integer.parseInt(item.get("sjid").toString());
			String spid = item.get("spid").toString();
			elset(sjid,spid);
		}
		return true;
	}
	
	public void elset(HashMap goods) {
		String _id = goods.get("spid").toString();
		Index index = new Index.Builder(goods).index("goods").type("goods").id(_id).build();
		try {
			DocumentResult result = jestClient.execute(index);		
			System.out.println(result);
		} catch (IOException e) {
			e.printStackTrace();			
		}
	}
	
	public boolean eldel(int sjid,String spid) throws IOException{
		String id = spid+"-"+sjid;
		Delete.Builder builder = new Delete.Builder(id);
		//builder.setHeader(PWDKEY, getSecret());
		//builder.refresh(true);
		Delete delete =  builder.index("goods").type("goods").build();
		JestResult result = jestClient.execute(delete);
		if(result != null && !result.isSucceeded()) {
			return false;
		}
		return true;
	}
	
	public HashMap elsearch(int sjid,String spid){
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		//searchSourceBuilder.query(QueryBuilders.queryStringQuery(spid));
		//searchSourceBuilder.query(QueryBuilders.matchQuery("_id",spid));
		searchSourceBuilder.query(QueryBuilders.wildcardQuery("_id",("*"+spid+"*").toLowerCase()));//模糊查询
		
		//searchSourceBuilder.sort("id", SortOrder.DESC);
		Search search = new Search.Builder("")//searchSourceBuilder.toString())
				.addIndex("goods").addType("goods").build();
		Get get = new Get.Builder("goods", spid).type("goods").build();
		try {
			JestResult result = jestClient.execute(get);
			System.out.println(result);
			//return result.getSourceAsObjectList(HashMap.class);
			return result.getSourceAsObject(HashMap.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;		
	}	
	
	public List<HashMap> firstpage(int sjid,int qyid,String dwbh,int lbid,int rows){
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		//searchSourceBuilder.query(QueryBuilders.wildcardQuery("_id",("*"+spid+"*").toLowerCase()));//模糊查询
		
//		searchSourceBuilder.query(QueryBuilders.matchQuery("goodsExtend.sjid",sjid));
//		searchSourceBuilder.query(QueryBuilders.matchQuery("goodsExtend.isVisible",true));
//		searchSourceBuilder.query(QueryBuilders.matchQuery("spid",spids));
		
//		String spids = getSpids(sjid,qyid,dwbh);
//		System.out.println(spids);
		String spids = "SPH00000009,SPH00000016,SPH00000037,SPH00000325,SPH00000706,SPH00000709,SPH00000806,SPH00000995,SPH00001133,SPH00001258,SPH00001424,SPH00001430,SPH00001508,SPH00001552,SPH00001553,SPH00001555,SPH00001563,SPH00001567,SPH00001569,SPH00001570,SPH00001596,SPH00001606,SPH00001612,SPH00001636,SPH00001637,SPH00001638,SPH00001643,SPH00001649,SPH00001651,SPH00001652,SPH00001655,SPH00001657,SPH00001658,SPH00001660,SPH00001661,SPH00001663,SPH00001665,SPH00001671,SPH00001672,SPH00001673,SPH00001676,SPH00001678,SPH00001681,SPH00001682,SPH00001689,SPH00001690,SPH00001691,SPH00001693,SPH00001702,SPH00001703,SPH00001704,SPH00001705,SPH00001706,SPH00001707,SPH00001708,SPH00001709,SPH00001710,SPH00001715,SPH00001716,SPH00001718,SPH00001719,SPH00001720,SPH00001721,SPH00001722,SPH00001723,SPH00001724,SPH00001725,SPH00001726,SPH00001728,SPH00001729,SPH00001738,SPH00001742,SPH00001749,SPH00001752,SPH00001753,SPH00001755,SPH00001756,SPH00001757,SPH00001758,SPH00001759,SPH00001760,SPH00001761,SPH00001762,SPH00001763,SPH00001765,SPH00001767,SPH00001771,SPH00001772,SPH00001777,SPH00001782,SPH00001793,SPH00001794,SPH00001795,SPH00001796,SPH00001798,SPH00001799,SPH00001800,SPH00001803,SPH00001804,SPH00001805,SPH00001806,SPH00001808,SPH00001809,SPH00001810,SPH00001811,SPH00001812,SPH00001813,SPH00001816,SPH00001817,SPH00001818,SPH00001820,SPH00001823,SPH00001825,SPH00001826,SPH00001830,SPH00001832,SPH00001843,SPH00001847,SPH00001857,SPH00001858,SPH00001861,SPH00001862,SPH00001864,SPH00001865,SPH00001868,SPH00001872,SPH00001879,SPH00001881,SPH00001882,SPH00001883,SPH00001885,SPH00001890,SPH00001894,SPH00001896,SPH00001897,SPH00001898,SPH00001899,SPH00001900,SPH00001901,SPH00001908,SPH00001913,SPH00001922,SPH00001923,SPH00001924,SPH00001927,SPH00001928,SPH00001929,SPH00001931,SPH00001932,SPH00001933,SPH00001934,SPH00001935,SPH00001942,SPH00001943,SPH00001945,SPH00001946,SPH00001947,SPH00001949,SPH00001951,SPH00001952,SPH00001955,SPH00001961,SPH00001970,SPH00001974,SPH00001980,SPH00001982,SPH00001983,SPH00001984,SPH00001987,SPH00001992,SPH00001994,SPH00001999,SPH00002001,SPH00002003,SPH00002004,SPH00002006,SPH00002008,SPH00002010,SPH00002012,SPH00002013,SPH00002015,SPH00002023,SPH00002029,SPH00002044,SPH00002046,SPH00002048,SPH00002049,SPH00002050,SPH00002051,SPH00002053,SPH00002059,SPH00002061,SPH00002089,SPH00002091,SPH00002114,SPH00002115,SPH00002126,SPH00002129,SPH00002130,SPH00002131,SPH00002132,SPH00002133,SPH00002134,SPH00002135,SPH00002136,SPH00002137,SPH00002138,SPH00002139,SPH00002140,SPH00002141,SPH00002142,SPH00002143,SPH00002144,SPH00002145,SPH00002146,SPH00002147,SPH00002148,SPH00002149,SPH00002150,SPH00002151,SPH00002152,SPH00002153,SPH00002154,SPH00002155,SPH00002156,SPH00002157,SPH00002158,SPH00002159,SPH00002160,SPH00002161,SPH00002162,SPH00002163,SPH00002164,SPH00002165,SPH00002166,SPH00002167,SPH00002168,SPH00002169,SPH00002170,SPH00002171,SPH00002172,SPH00002173,SPH00002174,SPH00002175,SPH00002176,SPH00002177,SPH00002178,SPH00002179,SPH00002180,SPH00002181,SPH00002182,SPH00002183,SPH00002186,SPH00002188,SPH00002190,SPH00002192,SPH00002214,SPH00002220,SPH00002221,SPH00002223,SPH00002230,SPH00002234,SPH00002236,SPH00002250,SPH00002259,SPH00002260,SPH00002261,SPH00002264,SPH00002268,SPH00002269,SPH00002270,SPH00002271,SPH00002272,SPH00002273,SPH00002274,SPH00002281,SPH00002283,SPH00002285,SPH00002286,SPH00002287,SPH00002289,SPH00002294,SPH00002304,SPH00002306,SPH00002307,SPH00002309,SPH00002310,SPH00002311,SPH00002312,SPH00002315,SPH00002318,SPH00002320,SPH00002322,SPH00002323,SPH00002324,SPH00002325,SPH00002326,SPH00002327,SPH00002328,SPH00002331,SPH00002333,SPH00002334,SPH00002335,SPH00002350,SPH00002354,SPH00002358,SPH00002361,SPH00002364,SPH00002365,SPH00002367,SPH00002368,SPH00002369,SPH00002371,SPH00002372,SPH00002373,SPH00002374,SPH00002376,SPH00002377,SPH00002378,SPH00002379,SPH00002380,SPH00002382,SPH00002383,SPH00002384,SPH00002386,SPH00002387,SPH00002390,SPH00002391,SPH00002392,SPH00002394,SPH00002401,SPH00002402,SPH00002403,SPH00002404,SPH00002405,SPH00002406,SPH00002407,SPH00002408,SPH00002409,SPH00002410,SPH00002411,SPH00002414,SPH00002415,SPH00002416,SPH00002417,SPH00002422,SPH00002458,SPH00002459,SPH00002461,SPH00002462,SPH00002463,SPH00002464,SPH00002474,SPH00002478,SPH00002479,SPH00002482,SPH00002483,SPH00002487,SPH00002488,SPH00002489,SPH00002493,SPH00002497,SPH00002502,SPH00002503,SPH00002507,SPH00002509,SPH00002510,SPH00002511,SPH00002512,SPH00002517,SPH00002525,SPH00002526,SPH00002527,SPH00002534,SPH00002535,SPH00002536,SPH00002537,SPH00002539,SPH00002541,SPH00002542,SPH00002543,SPH00002544,SPH00002549,SPH00002551,SPH00002552,SPH00002553,SPH00002555,SPH00002561,SPH00002562,SPH00002563,SPH00002564,SPH00002565,SPH00002567,SPH00002568,SPH00002569,SPH00002570,SPH00002571,SPH00002572,SPH00002574,SPH00002581,SPH00002582,SPH00002583,SPH00002584,SPH00002585,SPH00002586,SPH00002587,SPH00002590,SPH00002591,SPH00002592,SPH00002593,SPH00002595,SPH00002598,SPH00002599,SPH00002602,SPH00002604,SPH00002606,SPH00002607,SPH00002609,SPH00002628,SPH00002630,SPH00002633,SPH00002634,SPH00002635,SPH00002636,SPH00002637,SPH00002639,SPH00002644,SPH00002646,SPH00002650,SPH00002654,SPH00002655,SPH00002659,SPH00002660,SPH00002661,SPH00002663,SPH00002665,SPH00002672,SPH00002673,SPH00002674,SPH00002675,SPH00002677,SPH00002678,SPH00002679,SPH00002680,SPH00002682,SPH00002683,SPH00002686,SPH00002687,SPH00002688,SPH00002691,SPH00002693,SPH00002697,SPH00002707,SPH00002712,SPH00002713,SPH00002716,SPH00002717,SPH00002718,SPH00002719,SPH00002721,SPH00002722,SPH00002723,SPH00002724,SPH00002726,SPH00002729,SPH00002730,SPH00002731,SPH00002733,SPH00002738,SPH00002739,SPH00002740,SPH00002742,SPH00002743,SPH00002745,SPH00002747,SPH00002749,SPH00002751,SPH00002752,SPH00002754,SPH00002755,SPH00002756,SPH00002757,SPH00002760,SPH00002761,SPH00002762,SPH00002763,SPH00002766,SPH00002767,SPH00002769,SPH00002770,SPH00002771,SPH00002772,SPH00002773,SPH00002774,SPH00002775,SPH00002776,SPH00002778,SPH00002780,SPH00002781,SPH00002782,SPH00002783,SPH00002784,SPH00002785,SPH00002786,SPH00002787,SPH00002788,SPH00002789,SPH00002790,SPH00002791,SPH00002792,SPH00002797,SPH00002798,SPH00002799,SPH00002803,SPH00002804,SPH00002806,SPH00002807,SPH00002808,SPH00002809,SPH00002811,SPH00002812,SPH00002813,SPH00002814,SPH00002815,SPH00002816,SPH00002818,SPH00002819,SPH00002820,SPH00002821,SPH00002825,SPH00002829,SPH00002830,SPH00002831,SPH00002833,SPH00002834,SPH00002835,SPH00002836,SPH00002838,SPH00002839,SPH00002840,SPH00002841,SPH00002843,SPH00002844,SPH00002845,SPH00002846,SPH00002855,SPH00002856,SPH00002857,SPH00002858,SPH00002859,SPH00002861,SPH00002867,SPH00002874,SPH00002875,SPH00002876,SPH00002884,SPH00002885,SPH00002886,SPH00002887,SPH00002888,SPH00002890,SPH00002891,SPH00002892,SPH00002895,SPH00002896,SPH00002897,SPH00002900,SPH00002904,SPH00002906,SPH00002907,SPH00002908,SPH00002912,SPH00002913,SPH00002914,SPH00002915,SPH00002916,SPH00002920,SPH00002922,SPH00002923,SPH00002924,SPH00002926,SPH00002927,SPH00002928,SPH00002929,SPH00002930,SPH00002931,SPH00002932,SPH00002933,SPH00002934,SPH00002935,SPH00002936,SPH00002937,SPH00002938,SPH00002939,SPH00002940,SPH00002941,SPH00002942,SPH00002943,SPH00002945,SPH00002946,SPH00002947,SPH00002949,SPH00002950,SPH00002951,SPH00002953,SPH00002954,SPH00002955,SPH00002957,SPH00002958,SPH00002959,SPH00002960,SPH00002962,SPH00002963,SPH00002964,SPH00002965,SPH00002968,SPH00002969,SPH00002972,SPH00002973,SPH00002976,SPH00002977,SPH00002978,SPH00002979,SPH00002981,SPH00002984,SPH00002988,SPH00002989,SPH00002990,SPH00002991,SPH00002995,SPH00002996,SPH00002997,SPH00002998,SPH00003000,SPH00003002,SPH00003003,SPH00003005,SPH00003006,SPH00003007,SPH00003012,SPH00003015,SPH00003016,SPH00003017,SPH00003018,SPH00003020,SPH00003021,SPH00003022,SPH00003023,SPH00003024";

		searchSourceBuilder.query(QueryBuilders.boolQuery()
	            .must(QueryBuilders.matchQuery("ext.sjid",sjid))
	            .must(QueryBuilders.matchQuery("ext.isVisible",true))
	            .must(QueryBuilders.matchQuery("flbid",lbid))
	            .must(QueryBuilders.matchQuery("spid",spids)));
	        
		searchSourceBuilder.sort("hasBal", SortOrder.DESC);
		searchSourceBuilder.sort("ext.findex", SortOrder.ASC);
		searchSourceBuilder.from(0);
		searchSourceBuilder.size(rows);
		//searchSourceBuilder.sort("spid", SortOrder.ASC);
		System.out.println(searchSourceBuilder.toString());
		//System.out.println(queryBuilder.toString());
		Search search = new Search.Builder(searchSourceBuilder.toString())
				.addIndex("goods").addType("goods").build();
		//Get get = new Get.Builder("goods", spid).type("goods").build();
		try {
			JestResult result = jestClient.execute(search);
			System.out.println(result);
			return result.getSourceAsObjectList(HashMap.class);
			//return result.getSourceAsObject(HashMap.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;		
	}	
	
	public String getSpids(int sjid,int qyid,String dwbh) {
		List<String> spids;
		spids = goodsExtendMapper.getSpidsOldMchk(sjid, dwbh);
		if(spids.size()==0) {
			if(dwbh.substring(0, 6).equals("TESTDW")) {
				spids = goodsExtendMapper.getSpidsTestMchk(sjid, dwbh);
			}else {
				spids = goodsExtendMapper.getSpidsMchk(sjid, qyid,dwbh);
			}
		}
		String ret = "";
		for(String str : spids) {
			if(ret.length()!=0) {
				ret += ",";
			}
			ret += str;
		}
		return ret;
	}
	
	public HashMap searchall(int sjid,int qyid,String dwbh,int from,int size){
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		//searchSourceBuilder.query(QueryBuilders.wildcardQuery("_id",("*"+spid+"*").toLowerCase()));//模糊查询
		
//		searchSourceBuilder.query(QueryBuilders.matchQuery("goodsExtend.sjid",sjid));
//		searchSourceBuilder.query(QueryBuilders.matchQuery("goodsExtend.isVisible",true));
//		searchSourceBuilder.query(QueryBuilders.matchQuery("spid",spids));
		
		String spids = getSpids(sjid,qyid,dwbh);
		System.out.println(spids);
//		String spids = "SPH00000009,SPH00000016,SPH00000037,SPH00000325,SPH00000706,SPH00000709,SPH00000806,SPH00000995,SPH00001133,SPH00001258,SPH00001424,SPH00001430,SPH00001508,SPH00001552,SPH00001553,SPH00001555,SPH00001563,SPH00001567,SPH00001569,SPH00001570,SPH00001596,SPH00001606,SPH00001612,SPH00001636,SPH00001637,SPH00001638,SPH00001643,SPH00001649,SPH00001651,SPH00001652,SPH00001655,SPH00001657,SPH00001658,SPH00001660,SPH00001661,SPH00001663,SPH00001665,SPH00001671,SPH00001672,SPH00001673,SPH00001676,SPH00001678,SPH00001681,SPH00001682,SPH00001689,SPH00001690,SPH00001691,SPH00001693,SPH00001702,SPH00001703,SPH00001704,SPH00001705,SPH00001706,SPH00001707,SPH00001708,SPH00001709,SPH00001710,SPH00001715,SPH00001716,SPH00001718,SPH00001719,SPH00001720,SPH00001721,SPH00001722,SPH00001723,SPH00001724,SPH00001725,SPH00001726,SPH00001728,SPH00001729,SPH00001738,SPH00001742,SPH00001749,SPH00001752,SPH00001753,SPH00001755,SPH00001756,SPH00001757,SPH00001758,SPH00001759,SPH00001760,SPH00001761,SPH00001762,SPH00001763,SPH00001765,SPH00001767,SPH00001771,SPH00001772,SPH00001777,SPH00001782,SPH00001793,SPH00001794,SPH00001795,SPH00001796,SPH00001798,SPH00001799,SPH00001800,SPH00001803,SPH00001804,SPH00001805,SPH00001806,SPH00001808,SPH00001809,SPH00001810,SPH00001811,SPH00001812,SPH00001813,SPH00001816,SPH00001817,SPH00001818,SPH00001820,SPH00001823,SPH00001825,SPH00001826,SPH00001830,SPH00001832,SPH00001843,SPH00001847,SPH00001857,SPH00001858,SPH00001861,SPH00001862,SPH00001864,SPH00001865,SPH00001868,SPH00001872,SPH00001879,SPH00001881,SPH00001882,SPH00001883,SPH00001885,SPH00001890,SPH00001894,SPH00001896,SPH00001897,SPH00001898,SPH00001899,SPH00001900,SPH00001901,SPH00001908,SPH00001913,SPH00001922,SPH00001923,SPH00001924,SPH00001927,SPH00001928,SPH00001929,SPH00001931,SPH00001932,SPH00001933,SPH00001934,SPH00001935,SPH00001942,SPH00001943,SPH00001945,SPH00001946,SPH00001947,SPH00001949,SPH00001951,SPH00001952,SPH00001955,SPH00001961,SPH00001970,SPH00001974,SPH00001980,SPH00001982,SPH00001983,SPH00001984,SPH00001987,SPH00001992,SPH00001994,SPH00001999,SPH00002001,SPH00002003,SPH00002004,SPH00002006,SPH00002008,SPH00002010,SPH00002012,SPH00002013,SPH00002015,SPH00002023,SPH00002029,SPH00002044,SPH00002046,SPH00002048,SPH00002049,SPH00002050,SPH00002051,SPH00002053,SPH00002059,SPH00002061,SPH00002089,SPH00002091,SPH00002114,SPH00002115,SPH00002126,SPH00002129,SPH00002130,SPH00002131,SPH00002132,SPH00002133,SPH00002134,SPH00002135,SPH00002136,SPH00002137,SPH00002138,SPH00002139,SPH00002140,SPH00002141,SPH00002142,SPH00002143,SPH00002144,SPH00002145,SPH00002146,SPH00002147,SPH00002148,SPH00002149,SPH00002150,SPH00002151,SPH00002152,SPH00002153,SPH00002154,SPH00002155,SPH00002156,SPH00002157,SPH00002158,SPH00002159,SPH00002160,SPH00002161,SPH00002162,SPH00002163,SPH00002164,SPH00002165,SPH00002166,SPH00002167,SPH00002168,SPH00002169,SPH00002170,SPH00002171,SPH00002172,SPH00002173,SPH00002174,SPH00002175,SPH00002176,SPH00002177,SPH00002178,SPH00002179,SPH00002180,SPH00002181,SPH00002182,SPH00002183,SPH00002186,SPH00002188,SPH00002190,SPH00002192,SPH00002214,SPH00002220,SPH00002221,SPH00002223,SPH00002230,SPH00002234,SPH00002236,SPH00002250,SPH00002259,SPH00002260,SPH00002261,SPH00002264,SPH00002268,SPH00002269,SPH00002270,SPH00002271,SPH00002272,SPH00002273,SPH00002274,SPH00002281,SPH00002283,SPH00002285,SPH00002286,SPH00002287,SPH00002289,SPH00002294,SPH00002304,SPH00002306,SPH00002307,SPH00002309,SPH00002310,SPH00002311,SPH00002312,SPH00002315,SPH00002318,SPH00002320,SPH00002322,SPH00002323,SPH00002324,SPH00002325,SPH00002326,SPH00002327,SPH00002328,SPH00002331,SPH00002333,SPH00002334,SPH00002335,SPH00002350,SPH00002354,SPH00002358,SPH00002361,SPH00002364,SPH00002365,SPH00002367,SPH00002368,SPH00002369,SPH00002371,SPH00002372,SPH00002373,SPH00002374,SPH00002376,SPH00002377,SPH00002378,SPH00002379,SPH00002380,SPH00002382,SPH00002383,SPH00002384,SPH00002386,SPH00002387,SPH00002390,SPH00002391,SPH00002392,SPH00002394,SPH00002401,SPH00002402,SPH00002403,SPH00002404,SPH00002405,SPH00002406,SPH00002407,SPH00002408,SPH00002409,SPH00002410,SPH00002411,SPH00002414,SPH00002415,SPH00002416,SPH00002417,SPH00002422,SPH00002458,SPH00002459,SPH00002461,SPH00002462,SPH00002463,SPH00002464,SPH00002474,SPH00002478,SPH00002479,SPH00002482,SPH00002483,SPH00002487,SPH00002488,SPH00002489,SPH00002493,SPH00002497,SPH00002502,SPH00002503,SPH00002507,SPH00002509,SPH00002510,SPH00002511,SPH00002512,SPH00002517,SPH00002525,SPH00002526,SPH00002527,SPH00002534,SPH00002535,SPH00002536,SPH00002537,SPH00002539,SPH00002541,SPH00002542,SPH00002543,SPH00002544,SPH00002549,SPH00002551,SPH00002552,SPH00002553,SPH00002555,SPH00002561,SPH00002562,SPH00002563,SPH00002564,SPH00002565,SPH00002567,SPH00002568,SPH00002569,SPH00002570,SPH00002571,SPH00002572,SPH00002574,SPH00002581,SPH00002582,SPH00002583,SPH00002584,SPH00002585,SPH00002586,SPH00002587,SPH00002590,SPH00002591,SPH00002592,SPH00002593,SPH00002595,SPH00002598,SPH00002599,SPH00002602,SPH00002604,SPH00002606,SPH00002607,SPH00002609,SPH00002628,SPH00002630,SPH00002633,SPH00002634,SPH00002635,SPH00002636,SPH00002637,SPH00002639,SPH00002644,SPH00002646,SPH00002650,SPH00002654,SPH00002655,SPH00002659,SPH00002660,SPH00002661,SPH00002663,SPH00002665,SPH00002672,SPH00002673,SPH00002674,SPH00002675,SPH00002677,SPH00002678,SPH00002679,SPH00002680,SPH00002682,SPH00002683,SPH00002686,SPH00002687,SPH00002688,SPH00002691,SPH00002693,SPH00002697,SPH00002707,SPH00002712,SPH00002713,SPH00002716,SPH00002717,SPH00002718,SPH00002719,SPH00002721,SPH00002722,SPH00002723,SPH00002724,SPH00002726,SPH00002729,SPH00002730,SPH00002731,SPH00002733,SPH00002738,SPH00002739,SPH00002740,SPH00002742,SPH00002743,SPH00002745,SPH00002747,SPH00002749,SPH00002751,SPH00002752,SPH00002754,SPH00002755,SPH00002756,SPH00002757,SPH00002760,SPH00002761,SPH00002762,SPH00002763,SPH00002766,SPH00002767,SPH00002769,SPH00002770,SPH00002771,SPH00002772,SPH00002773,SPH00002774,SPH00002775,SPH00002776,SPH00002778,SPH00002780,SPH00002781,SPH00002782,SPH00002783,SPH00002784,SPH00002785,SPH00002786,SPH00002787,SPH00002788,SPH00002789,SPH00002790,SPH00002791,SPH00002792,SPH00002797,SPH00002798,SPH00002799,SPH00002803,SPH00002804,SPH00002806,SPH00002807,SPH00002808,SPH00002809,SPH00002811,SPH00002812,SPH00002813,SPH00002814,SPH00002815,SPH00002816,SPH00002818,SPH00002819,SPH00002820,SPH00002821,SPH00002825,SPH00002829,SPH00002830,SPH00002831,SPH00002833,SPH00002834,SPH00002835,SPH00002836,SPH00002838,SPH00002839,SPH00002840,SPH00002841,SPH00002843,SPH00002844,SPH00002845,SPH00002846,SPH00002855,SPH00002856,SPH00002857,SPH00002858,SPH00002859,SPH00002861,SPH00002867,SPH00002874,SPH00002875,SPH00002876,SPH00002884,SPH00002885,SPH00002886,SPH00002887,SPH00002888,SPH00002890,SPH00002891,SPH00002892,SPH00002895,SPH00002896,SPH00002897,SPH00002900,SPH00002904,SPH00002906,SPH00002907,SPH00002908,SPH00002912,SPH00002913,SPH00002914,SPH00002915,SPH00002916,SPH00002920,SPH00002922,SPH00002923,SPH00002924,SPH00002926,SPH00002927,SPH00002928,SPH00002929,SPH00002930,SPH00002931,SPH00002932,SPH00002933,SPH00002934,SPH00002935,SPH00002936,SPH00002937,SPH00002938,SPH00002939,SPH00002940,SPH00002941,SPH00002942,SPH00002943,SPH00002945,SPH00002946,SPH00002947,SPH00002949,SPH00002950,SPH00002951,SPH00002953,SPH00002954,SPH00002955,SPH00002957,SPH00002958,SPH00002959,SPH00002960,SPH00002962,SPH00002963,SPH00002964,SPH00002965,SPH00002968,SPH00002969,SPH00002972,SPH00002973,SPH00002976,SPH00002977,SPH00002978,SPH00002979,SPH00002981,SPH00002984,SPH00002988,SPH00002989,SPH00002990,SPH00002991,SPH00002995,SPH00002996,SPH00002997,SPH00002998,SPH00003000,SPH00003002,SPH00003003,SPH00003005,SPH00003006,SPH00003007,SPH00003012,SPH00003015,SPH00003016,SPH00003017,SPH00003018,SPH00003020,SPH00003021,SPH00003022,SPH00003023,SPH00003024";

		searchSourceBuilder.query(QueryBuilders.boolQuery()
	            .must(QueryBuilders.matchQuery("ext.sjid",sjid))
	            .must(QueryBuilders.matchQuery("ext.isVisible",true))
	            //.must(QueryBuilders.matchQuery("flbid",lbid))
	            .must(QueryBuilders.matchQuery("spid",spids)));//控销
	        
		searchSourceBuilder.sort("hasBal", SortOrder.DESC);
		searchSourceBuilder.sort("ext.findex", SortOrder.ASC);
		searchSourceBuilder.from(from);
		searchSourceBuilder.size(size);
		//searchSourceBuilder.sort("spid", SortOrder.ASC);
		System.out.println(searchSourceBuilder.toString());
		//System.out.println(queryBuilder.toString());
		Search search = new Search.Builder(searchSourceBuilder.toString())
				.addIndex("goods").addType("goods").build();
		//Get get = new Get.Builder("goods", spid).type("goods").build();
		try {
			JestResult result = jestClient.execute(search);
			System.out.println(result);
			String hits = result.getValue("hits").toString();
			System.out.println("hits:"+hits);
			int a = hits.indexOf("total=");
			int b = hits.indexOf(",", a);
			String ss = hits.substring(a+6, b);
			System.out.println("ss:"+ss);
	        int total = Integer.parseInt(new java.text.DecimalFormat("#").format(Double.parseDouble(ss)));
	        System.out.println("total的值为:"+total);	        
			
			HashMap map =new HashMap();
			map.put("total",total);
			map.put("from",from);
			map.put("size",size);
			map.put("detail",result.getSourceAsObjectList(HashMap.class));
			return map;
			//return result.getSourceAsObject(HashMap.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;		
	}
	
	public List<HashMap> searchallcount(int sjid,int qyid,String dwbh,int from,int size){
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		//searchSourceBuilder.query(QueryBuilders.wildcardQuery("_id",("*"+spid+"*").toLowerCase()));//模糊查询
		
//		searchSourceBuilder.query(QueryBuilders.matchQuery("goodsExtend.sjid",sjid));
//		searchSourceBuilder.query(QueryBuilders.matchQuery("goodsExtend.isVisible",true));
//		searchSourceBuilder.query(QueryBuilders.matchQuery("spid",spids));
		
		String spids = getSpids(sjid,qyid,dwbh);
		System.out.println(spids);
//		String spids = "SPH00000009,SPH00000016,SPH00000037,SPH00000325,SPH00000706,SPH00000709,SPH00000806,SPH00000995,SPH00001133,SPH00001258,SPH00001424,SPH00001430,SPH00001508,SPH00001552,SPH00001553,SPH00001555,SPH00001563,SPH00001567,SPH00001569,SPH00001570,SPH00001596,SPH00001606,SPH00001612,SPH00001636,SPH00001637,SPH00001638,SPH00001643,SPH00001649,SPH00001651,SPH00001652,SPH00001655,SPH00001657,SPH00001658,SPH00001660,SPH00001661,SPH00001663,SPH00001665,SPH00001671,SPH00001672,SPH00001673,SPH00001676,SPH00001678,SPH00001681,SPH00001682,SPH00001689,SPH00001690,SPH00001691,SPH00001693,SPH00001702,SPH00001703,SPH00001704,SPH00001705,SPH00001706,SPH00001707,SPH00001708,SPH00001709,SPH00001710,SPH00001715,SPH00001716,SPH00001718,SPH00001719,SPH00001720,SPH00001721,SPH00001722,SPH00001723,SPH00001724,SPH00001725,SPH00001726,SPH00001728,SPH00001729,SPH00001738,SPH00001742,SPH00001749,SPH00001752,SPH00001753,SPH00001755,SPH00001756,SPH00001757,SPH00001758,SPH00001759,SPH00001760,SPH00001761,SPH00001762,SPH00001763,SPH00001765,SPH00001767,SPH00001771,SPH00001772,SPH00001777,SPH00001782,SPH00001793,SPH00001794,SPH00001795,SPH00001796,SPH00001798,SPH00001799,SPH00001800,SPH00001803,SPH00001804,SPH00001805,SPH00001806,SPH00001808,SPH00001809,SPH00001810,SPH00001811,SPH00001812,SPH00001813,SPH00001816,SPH00001817,SPH00001818,SPH00001820,SPH00001823,SPH00001825,SPH00001826,SPH00001830,SPH00001832,SPH00001843,SPH00001847,SPH00001857,SPH00001858,SPH00001861,SPH00001862,SPH00001864,SPH00001865,SPH00001868,SPH00001872,SPH00001879,SPH00001881,SPH00001882,SPH00001883,SPH00001885,SPH00001890,SPH00001894,SPH00001896,SPH00001897,SPH00001898,SPH00001899,SPH00001900,SPH00001901,SPH00001908,SPH00001913,SPH00001922,SPH00001923,SPH00001924,SPH00001927,SPH00001928,SPH00001929,SPH00001931,SPH00001932,SPH00001933,SPH00001934,SPH00001935,SPH00001942,SPH00001943,SPH00001945,SPH00001946,SPH00001947,SPH00001949,SPH00001951,SPH00001952,SPH00001955,SPH00001961,SPH00001970,SPH00001974,SPH00001980,SPH00001982,SPH00001983,SPH00001984,SPH00001987,SPH00001992,SPH00001994,SPH00001999,SPH00002001,SPH00002003,SPH00002004,SPH00002006,SPH00002008,SPH00002010,SPH00002012,SPH00002013,SPH00002015,SPH00002023,SPH00002029,SPH00002044,SPH00002046,SPH00002048,SPH00002049,SPH00002050,SPH00002051,SPH00002053,SPH00002059,SPH00002061,SPH00002089,SPH00002091,SPH00002114,SPH00002115,SPH00002126,SPH00002129,SPH00002130,SPH00002131,SPH00002132,SPH00002133,SPH00002134,SPH00002135,SPH00002136,SPH00002137,SPH00002138,SPH00002139,SPH00002140,SPH00002141,SPH00002142,SPH00002143,SPH00002144,SPH00002145,SPH00002146,SPH00002147,SPH00002148,SPH00002149,SPH00002150,SPH00002151,SPH00002152,SPH00002153,SPH00002154,SPH00002155,SPH00002156,SPH00002157,SPH00002158,SPH00002159,SPH00002160,SPH00002161,SPH00002162,SPH00002163,SPH00002164,SPH00002165,SPH00002166,SPH00002167,SPH00002168,SPH00002169,SPH00002170,SPH00002171,SPH00002172,SPH00002173,SPH00002174,SPH00002175,SPH00002176,SPH00002177,SPH00002178,SPH00002179,SPH00002180,SPH00002181,SPH00002182,SPH00002183,SPH00002186,SPH00002188,SPH00002190,SPH00002192,SPH00002214,SPH00002220,SPH00002221,SPH00002223,SPH00002230,SPH00002234,SPH00002236,SPH00002250,SPH00002259,SPH00002260,SPH00002261,SPH00002264,SPH00002268,SPH00002269,SPH00002270,SPH00002271,SPH00002272,SPH00002273,SPH00002274,SPH00002281,SPH00002283,SPH00002285,SPH00002286,SPH00002287,SPH00002289,SPH00002294,SPH00002304,SPH00002306,SPH00002307,SPH00002309,SPH00002310,SPH00002311,SPH00002312,SPH00002315,SPH00002318,SPH00002320,SPH00002322,SPH00002323,SPH00002324,SPH00002325,SPH00002326,SPH00002327,SPH00002328,SPH00002331,SPH00002333,SPH00002334,SPH00002335,SPH00002350,SPH00002354,SPH00002358,SPH00002361,SPH00002364,SPH00002365,SPH00002367,SPH00002368,SPH00002369,SPH00002371,SPH00002372,SPH00002373,SPH00002374,SPH00002376,SPH00002377,SPH00002378,SPH00002379,SPH00002380,SPH00002382,SPH00002383,SPH00002384,SPH00002386,SPH00002387,SPH00002390,SPH00002391,SPH00002392,SPH00002394,SPH00002401,SPH00002402,SPH00002403,SPH00002404,SPH00002405,SPH00002406,SPH00002407,SPH00002408,SPH00002409,SPH00002410,SPH00002411,SPH00002414,SPH00002415,SPH00002416,SPH00002417,SPH00002422,SPH00002458,SPH00002459,SPH00002461,SPH00002462,SPH00002463,SPH00002464,SPH00002474,SPH00002478,SPH00002479,SPH00002482,SPH00002483,SPH00002487,SPH00002488,SPH00002489,SPH00002493,SPH00002497,SPH00002502,SPH00002503,SPH00002507,SPH00002509,SPH00002510,SPH00002511,SPH00002512,SPH00002517,SPH00002525,SPH00002526,SPH00002527,SPH00002534,SPH00002535,SPH00002536,SPH00002537,SPH00002539,SPH00002541,SPH00002542,SPH00002543,SPH00002544,SPH00002549,SPH00002551,SPH00002552,SPH00002553,SPH00002555,SPH00002561,SPH00002562,SPH00002563,SPH00002564,SPH00002565,SPH00002567,SPH00002568,SPH00002569,SPH00002570,SPH00002571,SPH00002572,SPH00002574,SPH00002581,SPH00002582,SPH00002583,SPH00002584,SPH00002585,SPH00002586,SPH00002587,SPH00002590,SPH00002591,SPH00002592,SPH00002593,SPH00002595,SPH00002598,SPH00002599,SPH00002602,SPH00002604,SPH00002606,SPH00002607,SPH00002609,SPH00002628,SPH00002630,SPH00002633,SPH00002634,SPH00002635,SPH00002636,SPH00002637,SPH00002639,SPH00002644,SPH00002646,SPH00002650,SPH00002654,SPH00002655,SPH00002659,SPH00002660,SPH00002661,SPH00002663,SPH00002665,SPH00002672,SPH00002673,SPH00002674,SPH00002675,SPH00002677,SPH00002678,SPH00002679,SPH00002680,SPH00002682,SPH00002683,SPH00002686,SPH00002687,SPH00002688,SPH00002691,SPH00002693,SPH00002697,SPH00002707,SPH00002712,SPH00002713,SPH00002716,SPH00002717,SPH00002718,SPH00002719,SPH00002721,SPH00002722,SPH00002723,SPH00002724,SPH00002726,SPH00002729,SPH00002730,SPH00002731,SPH00002733,SPH00002738,SPH00002739,SPH00002740,SPH00002742,SPH00002743,SPH00002745,SPH00002747,SPH00002749,SPH00002751,SPH00002752,SPH00002754,SPH00002755,SPH00002756,SPH00002757,SPH00002760,SPH00002761,SPH00002762,SPH00002763,SPH00002766,SPH00002767,SPH00002769,SPH00002770,SPH00002771,SPH00002772,SPH00002773,SPH00002774,SPH00002775,SPH00002776,SPH00002778,SPH00002780,SPH00002781,SPH00002782,SPH00002783,SPH00002784,SPH00002785,SPH00002786,SPH00002787,SPH00002788,SPH00002789,SPH00002790,SPH00002791,SPH00002792,SPH00002797,SPH00002798,SPH00002799,SPH00002803,SPH00002804,SPH00002806,SPH00002807,SPH00002808,SPH00002809,SPH00002811,SPH00002812,SPH00002813,SPH00002814,SPH00002815,SPH00002816,SPH00002818,SPH00002819,SPH00002820,SPH00002821,SPH00002825,SPH00002829,SPH00002830,SPH00002831,SPH00002833,SPH00002834,SPH00002835,SPH00002836,SPH00002838,SPH00002839,SPH00002840,SPH00002841,SPH00002843,SPH00002844,SPH00002845,SPH00002846,SPH00002855,SPH00002856,SPH00002857,SPH00002858,SPH00002859,SPH00002861,SPH00002867,SPH00002874,SPH00002875,SPH00002876,SPH00002884,SPH00002885,SPH00002886,SPH00002887,SPH00002888,SPH00002890,SPH00002891,SPH00002892,SPH00002895,SPH00002896,SPH00002897,SPH00002900,SPH00002904,SPH00002906,SPH00002907,SPH00002908,SPH00002912,SPH00002913,SPH00002914,SPH00002915,SPH00002916,SPH00002920,SPH00002922,SPH00002923,SPH00002924,SPH00002926,SPH00002927,SPH00002928,SPH00002929,SPH00002930,SPH00002931,SPH00002932,SPH00002933,SPH00002934,SPH00002935,SPH00002936,SPH00002937,SPH00002938,SPH00002939,SPH00002940,SPH00002941,SPH00002942,SPH00002943,SPH00002945,SPH00002946,SPH00002947,SPH00002949,SPH00002950,SPH00002951,SPH00002953,SPH00002954,SPH00002955,SPH00002957,SPH00002958,SPH00002959,SPH00002960,SPH00002962,SPH00002963,SPH00002964,SPH00002965,SPH00002968,SPH00002969,SPH00002972,SPH00002973,SPH00002976,SPH00002977,SPH00002978,SPH00002979,SPH00002981,SPH00002984,SPH00002988,SPH00002989,SPH00002990,SPH00002991,SPH00002995,SPH00002996,SPH00002997,SPH00002998,SPH00003000,SPH00003002,SPH00003003,SPH00003005,SPH00003006,SPH00003007,SPH00003012,SPH00003015,SPH00003016,SPH00003017,SPH00003018,SPH00003020,SPH00003021,SPH00003022,SPH00003023,SPH00003024";

		searchSourceBuilder.query(QueryBuilders.boolQuery()
	            .must(QueryBuilders.matchQuery("ext.sjid",sjid))
	            .must(QueryBuilders.matchQuery("ext.isVisible",true))
	            //.must(QueryBuilders.matchQuery("flbid",lbid))
	            .must(QueryBuilders.matchQuery("spid",spids)));//控销
	        
//		searchSourceBuilder.sort("hasBal", SortOrder.DESC);
//		searchSourceBuilder.sort("ext.findex", SortOrder.ASC);
//		searchSourceBuilder.from(from);
		searchSourceBuilder.size(0);
		//searchSourceBuilder.sort("spid", SortOrder.ASC);
		System.out.println(searchSourceBuilder.toString());
		//System.out.println(queryBuilder.toString());
		Search search = new Search.Builder(searchSourceBuilder.toString())
				.addIndex("goods").addType("goods").build();
		//Get get = new Get.Builder("goods", spid).type("goods").build();
		try {
			JestResult result = jestClient.execute(search);
			System.out.println(result);
			
			String hits = result.getValue("hits").toString();
			System.out.println(hits);
			//Gson gson = new Gson();
	        Map<String, Object> map = new HashMap<String, Object>();
	        map = gson.fromJson(hits, map.getClass());
	        
	        String ss=map.get("total").toString();
	        int total = Integer.parseInt(new java.text.DecimalFormat("0").format(map.get("total")));
	        System.out.println("total的值为:"+total);
	        
			return result.getSourceAsObjectList(HashMap.class);
			//return result.getSourceAsObject(HashMap.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;		
	}
}
